/** @namespace app.model.Project */
module.exports = app => {
  const {STRING, INTEGER, DATE, TEXT, JSON, BOOLEAN} = app.Sequelize;

  const Project = app.model.define('project', {
    name: {type: STRING(30)},
    start_at: {type: DATE, allowNull: false, defaultValue: app.Sequelize.NOW},
    end_at: {type: DATE, allowNull: false, defaultValue: '2099-01-01 00:00:00'},
    email: TEXT,
    options: {
      type: JSON,
      defaultValue: {
        aggregation: [],
        spiderAggregation: [],
        color: '#66b1ff',
        hide: {cost: true, click: true, added: true, reflux: true, danmu: true},
      },
    },
    delete: {type: BOOLEAN, defaultValue: false},
  }, {defaultScope: {where: {delete: false}}});

  Project.associate = () => {

    app.model.Project.hasMany(app.model.DetailAnchorSchedule,
        {as: 'schedule', foreignKey: 'project_id'});

    app.model.Project.belongsToMany(app.model.DetailAnchor, {
      as: 'anchor',
      through: {model: app.model.ProjectAnchor},
    });
    app.model.DetailAnchor.belongsToMany(app.model.Project, {
          through: {model: app.model.ProjectAnchor},
          foreignKey: 'anchor_id',
        },
    );

    app.model.ProjectAnchor.belongsTo(app.model.DetailAnchor,
        {as: 'anchor', foreignKey: 'anchor_id'});
    app.model.ProjectAnchor.belongsTo(app.model.Project);
  };

  return Project;
};